package com.dhcc.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.entity.ClientUser;
import com.dhcc.entity.SysDepartment;
import com.dhcc.entity.SysUserDepartmentInstitution;
import com.dhcc.entity.SysUserPosition;
import com.dhcc.httpApiService.IdGeneratorService;
import com.dhcc.mapper.ClientUserMapper;
import com.dhcc.mapper.SysDepartmentMapper;
import com.dhcc.mapper.SysUserDepartmentInstitutionMapper;
import com.dhcc.mapper.SysUserPositionMapper;
import com.dhcc.service.ClientUserService;
import com.dhcc.utils.AuthUtil;
import com.dhcc.utils.PasswordEncoderUtil;
import com.dhcc.vo.ClientUserInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ClientUserServiceImpl extends ServiceImpl<ClientUserMapper, ClientUser> implements ClientUserService {
    @Resource
    private ClientUserMapper clientUserMapper;
    @Resource
    private PasswordEncoderUtil passwordEncoderUtil;
    @Resource
    private SysUserDepartmentInstitutionMapper sysUserDepartmentInstitutionMapper;
    @Resource
    private SysDepartmentMapper sysDepartmentMapper;
    @Resource
    private SysUserPositionMapper sysUserPositionMapper;
    @Override
    public ClientUser getUserById(String userId) {
        return clientUserMapper.getUserInfo(userId);
    }

    @Override
    public Integer addUser(ClientUser clientUser) {
        //添加用户默认帐号状态正常
        clientUser.setStatus((byte) 0);
        //添加用户默认账户存在
        clientUser.setDelFlag((byte) 0);
        //对用户密码进行加密
        String encode = passwordEncoderUtil.encode(clientUser.getUserPassword());
        clientUser.setUserPassword(encode);
        //添加创建者
        String userId = AuthUtil.getUserId();
        //通过userid查询对应的用户姓名
        ClientUser creatUser = this.getUserById(userId);
        clientUser.setCreateUser(creatUser.getName());
        //添加创建时间
        clientUser.setCreateTime(String.valueOf(LocalDateTime.now()));
        //在用户表中添加用户
        Integer addResult=clientUserMapper.addUser(clientUser);
        return addResult;
    }

    @Override
    public Integer deleteUserByUserId(String userId) {
        return clientUserMapper.deleteUserByUserId(userId);
    }

    @Override
    public ClientUserInfo selectUserByUserIdAndInstitutionId(String userId, String institutionId,String institutionRoleId) {
        ClientUser user = this.getUserById(userId);
        ClientUserInfo clientUserInfo=new ClientUserInfo();
        BeanUtils.copyProperties(user,clientUserInfo);
        //通过用户编号查询当前用户在此机构的部门和岗位
        SysUserDepartmentInstitution sysUserDepartmentInstitution=sysUserDepartmentInstitutionMapper.getDepartmentIdAndPositionId(userId,institutionId,institutionRoleId);
        //通过部门编号查询部门信息
        SysDepartment department = sysDepartmentMapper.getDepartment(sysUserDepartmentInstitution.getDepartmentId());
        clientUserInfo.setDepartmentName(department.getDeptName());
        //通过岗位编号查询岗位信息
        SysUserPosition position = sysUserPositionMapper.getPositionName(sysUserDepartmentInstitution.getPositionId());
        clientUserInfo.setPositionName(position.getPositionName());
        return clientUserInfo;
    }

    @Override
    public Integer updateUser(ClientUser clientUser) {
        ClientUser user = this.getUserById(AuthUtil.getUserId());
        clientUser.setUpdateUser(user.getName());
        clientUser.setUpdateTime(LocalDateTime.now().toString());
        return clientUserMapper.updateUser(clientUser);
    }
}
